package com.view.pages.worldtown.objects
{
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import org.papervision3d.materials.*;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.Collada;

	public class AsosTower extends DisplayObject3D
	{
		private var $loadedXML:Object;
		private var $loadedImages:Object;
		private var $settings:Object;
		
		private var $asosRoom:Collada;
		private var $asos:Collada;

		private var $doorsMC:MovieClip;
		private var $dynamicDoors:Doors3D;
		
		private var isIn:Boolean;
		private var $position:String;
		
		private var $dW:Number;
		private var	$dH:Number;
		
		
		public function AsosTower(_loadedXML:Object,_loadedImages:Object,_settings:Object)
		{
			$loadedXML = _loadedXML;
			$loadedImages = _loadedImages;
			$settings = _settings;
			super();
			init();
		}
		private function init():void
		{
			$dW = 2860;
			$dH = 1620;
			
			//ASOS FRONT
			$asos = new Collada($loadedXML.asos);
			$asos.z = -9745;
			addChild($asos);
			
			//ASOS ROOM
			$asosRoom = new Collada($loadedXML.asosRoom);
			//$asosRoom.useOwnContainer = true;
			//$asosRoom.z = -9745;
			//addChild($asosRoom);
			
			//DOORS MC
			$doorsMC = new DoorsMC();
			$dynamicDoors = new Doors3D(DoorsMC,$loadedImages.doors,$dW,$dH);
			$dynamicDoors.addEventListener(Doors3D.DOORS_CLICKED,onClick);
			$dynamicDoors.addEventListener(Doors3D.DOORS_OPENED,onOpen);
			$dynamicDoors.addEventListener(Doors3D.DOORS_CLOSED,onClose);
			$dynamicDoors.addDoorsListener();
			$dynamicDoors.start();
			$dynamicDoors.y = $dH/2;
			//$dynamicDoors.rotationY = 90;
			$dynamicDoors.z = 30;
			addChild($dynamicDoors);
		}
		private function onClick(_e:Event):void
		{
			//dispatchEvent(new Event(DOORS_CLICKED));
			addChild($asosRoom);
			$dynamicDoors.open();
			$asos.useOwnContainer = true;
			$asosRoom.useOwnContainer = true;	
			$dynamicDoors.useOwnContainer = true;
		}
		private function onOpen(_e:Event):void
		{
			$dynamicDoors.close();	
			//dispatchEvent(new Event(DOORS_OPENED));
		}
		private function onClose(_e:Event):void
		{
			trace("cam:"+$settings.camera+" ; this:"+this);
			if($position == "left")
			{
				if($settings.camera.x<this.x)
				{
					personCheck(true);
				}	
				else
				{
					personCheck(false);
				}
			}
			if($position == "right")
			{
				if($settings.camera.x>this.x)
				{
					personCheck(true);
				}	
				else
				{
					personCheck(false);
				}
			}
			if($position == "back")
			{
				if($settings.camera.z<this.z)
				{
					personCheck(true);
				}	
				else
				{
					personCheck(false);
				}
			}
			//dispatchEvent(new Event(DOORS_CLOSED));
		}
		private function personCheck(_in:Boolean):void
		{
			if(!_in)
			{
				isIn = false;
				removeChild($asosRoom);
			}
			else
			{
				isIn = true;
			}
		}
	}
}